import { Component, OnInit, Input } from '@angular/core';
import {Student} from '../student';
import {StudentService} from '../students.service';
import {ActivatedRoute} from '@angular/router';
import {Location} from '@angular/common';

@Component({
  selector: 'app-student-detail',
  templateUrl: './student-detail.component.html',
  styleUrls: ['./student-detail.component.css']
})
export class StudentDetailComponent implements OnInit {
  @Input() student: Student;
  constructor(private studentsService: StudentService,
              private route: ActivatedRoute,
              private location: Location
  ) { }

  ngOnInit(): void {
    this.getStudent();
  }
  getStudent(): void {
    const id: string = String(this.route.snapshot.paramMap.get('id'));
    this.studentsService.getStudent(id)
      .subscribe( student => this.student = student);
  }
  goBack(): void {
    this.location.back();
  }
  save(): void {
    this.studentsService.updateStudent(this.student)
      .subscribe(() => this.goBack());
  }

}
